import router from "@/router/index.js";
import {useRoute} from "vue-router";
import dayjs from "dayjs";

export const uuid = () => {
    const s4 = () => {
        return Math.floor((1 + Math.random()) * 0x10000)
            .toString(16)
            .substring(1);
    };

    return (
        s4() +
        s4() +
        '-' +
        s4() +
        '-' +
        s4() +
        '-' +
        s4() +
        '-' +
        s4() +
        s4() +
        s4()
    );
}

export const navigateTo = (path, query = {}) => {
    router.push({path, query})
}

export const getQuery = (key) => {
    const route = useRoute()
    return route.query[key]?.toString()
}

// 根据字段获取其 label 值
export const getDictLabelByField = (field, data) => {

    if (!String(field) || !data) {
        return ''
    }

    const item = data.find(item => item.value === String(field))

    return item?.label || ''
}

// 获取当前时间
export const getCurrentTime = () => {
    return dayjs().format('YYYY-MM-DD HH:mm:ss')
}

// table 计算行总和
export const calculateTotal = (row, keys) => {
    return keys.reduce((sum, key) => sum + (row[key] || 0), 0);
}

// 随机数值生成
export const randomNumberInRange = (min = 1, max = 10) => {
    // 将传入的 min 和 max 转换为整数，以防止传入的是浮点数
    min = Math.ceil(min);
    max = Math.floor(max);

    // 生成一个介于 min 和 max（包括 min 和 max）之间的随机整数
    return Math.floor(Math.random() * (max - min + 1)) + min;}